﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using KPWeb.Class;
using KPWeb.Data;
using KPWeb.Function;
using Microsoft.Vbe.Interop;


namespace KPWeb.AnkietaOcenaPracownika
{
    public partial class AnkietaMojaOcena : KPPage
    {




        string[]  B1 = new string[] {"lB1_1", "lB1_2", "lB1_3", "lB1_4", "lB1_5", "lB1_6", "lB1_7", "lB1_8", "lB1_9"};
        int[]    wB1 = new int[] { 50, 15, 5, 7, 4, 2, 5, 3, 2 };
        int B1Waga = 8;
        private void komunikatError(int kod)
        {
            fError.Visible = true;
            switch (kod)
            {
                case -1: Error1.Visible = true;
                    break;
                case -2: Error2.Visible = true;
                    break;
                case -3: Error3.Visible = true;
                    break;
                case -4: Error4.Visible = true;
                    break;
                case -5: Error4.Visible = true;
                    break;
                default: fError.Visible = false;
                    break;
            }
        }
        //internal static Control getControlByName(Control ctrl, string ctlName)
        //{
        //    foreach (Control child in ctrl.Controls)
        //    {
        //        if (String.Compare(child.Name, ctlName, false) == 0)
        //            return child;
        //        else if (child.HasChildren)
        //        {
        //            Control retChild = getControlByName(child, ctlName);
        //            if (retChild != null)
        //                return retChild;
        //        }
        //    }
        //    return null;
        //}

        //static void Main(string[] args)
        //{

        //    AnkietaPola ap = new AnkietaPola();
        //    ap.A1 = 2;
        //    ap.C1 = 23;
        //    ap.D1 = "sasa";
        //    ap.SekcjaE1.E1_1 = 33;

        //    ap.SekcjaE1.SekcjaE1_3.E1_3_1 = 5;
        //    XmlSerializer x = new XmlSerializer(typeof(AnkietaPola));
        //    StringWriter sw = new StringWriter();
        //    x.Serialize(sw, ap);
        //    Console.WriteLine(sw);


        //    AnkietaPola ap2 = new AnkietaPola();
        //    ap2 = x.Deserialize(new StringReader(sw.ToString())) as AnkietaPola;
        //    Console.WriteLine(ap2.D1);
        //}




        private void InitFormularzAnkietaOcenaXML(KP_AnkietyOcenaXML wiz)
        {
            XmlSerializer x = new XmlSerializer(typeof(AnkietaPola));
            AnkietaPola ap = x.Deserialize(new StringReader(wiz.AnkietaOcenaXML)) as AnkietaPola;
            XElement xml = XElement.Load(new StringReader(wiz.AnkietaOcenaXML));
          //  decimal? sum = xml.Elements("nB").Elements("nB1").Elements().Sum(a => decimal.Parse(a.Value)); #### Michal
            int iSum = 0;// (int)sum;
          
             //   B1_1.Text = xml.Elements("nB").Elements("nB1").get
                B1_1.Text = ap.nB.nB1.B1_1.ToString();
                B1_2.Text = ap.nB.nB1.B1_2.ToString();
                B1_3.Text = ap.nB.nB1.B1_3.ToString();
                B1_4.Text = ap.nB.nB1.B1_4.ToString();
                B1_5.Text = ap.nB.nB1.B1_5.ToString();
                B1_6.Text = ap.nB.nB1.B1_6.ToString();
                B1_7.Text = ap.nB.nB1.B1_7.ToString();
                B1_8.Text = ap.nB.nB1.B1_8.ToString();
                B1_9.Text = ap.nB.nB1.B1_9.ToString();
           // }
            //B1_5.Text = wiz.B1_5 == null ? "" : wiz.B1_5.ToString();
            //B1_6.Text = wiz.B1_6 == null ? "" : wiz.B1_6.ToString();
            //B1_7.Text = wiz.B1_7 == null ? "" : wiz.B1_7.ToString();
            //B1_8.Text = wiz.B1_8 == null ? "" : wiz.B1_8.ToString();
            //B1_9.Text = wiz.B1_9 == null ? "" : wiz.B1_9.ToString();

           string strName = "wB1";// numeracja od 1 do 9
           string strNameTemp = "";
           int i = 0;
           for (int s = 0; s < B1.Count(); s++)//(s+1).ToString() 
                    {
                        strNameTemp = (strName.Substring(1, 2) + '_' + (s + 1).ToString());
                        Label lbl = this.FindControl("l" + strNameTemp) as Label;
                        TextBox tbl = this.FindControl(strNameTemp) as TextBox;
                        //if (tbl!=null && tbl.Text!="")
                        //    lbl.Text = (wB1[s]*int.Parse(tbl.Text)).ToString();
                      //  TextBox textB= this.FindControl(strNameTemp) as TextBox;
                      
                        if (lbl != null)
                        {
                            if (tbl != null)
                            {

                                foreach (Control t in FindControlSuma(PanelB, typeof(TextBox)))
                                {
                                    if (t is TextBox && t.ID == tbl.ID)
                                    {
                                        i = int.Parse(tbl.Text == "" ? "0" : tbl.Text);
                                        break;
                                    }

                                }
                            }
                         
                            foreach (Control c in FindControlSuma(PanelB, typeof(Label)))
                            {
                                if (c is Label && c.ID == lbl.ID)
                                {
                                    iSum += wB1[s] * i;
                                    lbl.Text = (wB1[s] * i).ToString();
                                    break;
                                }

                            }
                        }
                        //* int.Parse(tbl.Text)      
                    }

           lB1_Suma.Text = iSum.ToString();
           lB1_SumaWaga.Text = (Math.Round((iSum * B1Waga) / 100.0, 2)).ToString();




            
        }
        private void InitFormularzAnkietaOcena(KP_AnkietyOcena wiz)
        {
            int i;
            int sum;
            //string literals = "BCD";
            //int first = 20;
            //int second = 30;
            //foreach (char literal in literals)
            //{
            //    for (int f = 0; f < first; f++)
            //    {
            //        for (int s = 0; s < second; s++)
            //        {
            //           TextBox t=this.FindControlRec(this,literal + f.ToString() + "_" + s.ToString()) as TextBox;
            //           if (t != null)
            //           {
            //               string x = wiz.B1_1.Value.ToString();
            //               int start = x.IndexOf(literal + f.ToString() + "_" + s.ToString());
            //               int end=

            //           }
            //        }
            //    }

            //}




            B1_1.Text = wiz.B1_1 == null ? "" : wiz.B1_1.ToString();
            B1_2.Text = wiz.B1_2 == null ? "" : wiz.B1_2.ToString();
            B1_3.Text = wiz.B1_3 == null ? "" : wiz.B1_3.ToString();
            B1_4.Text = wiz.B1_4 == null ? "" : wiz.B1_4.ToString();
            //B1_5.Text = wiz.B1_5 == null ? "" : wiz.B1_5.ToString();
            //B1_6.Text = wiz.B1_6 == null ? "" : wiz.B1_6.ToString();
            //B1_7.Text = wiz.B1_7 == null ? "" : wiz.B1_7.ToString();
            //B1_8.Text = wiz.B1_8 == null ? "" : wiz.B1_8.ToString();
            //B1_9.Text = wiz.B1_9 == null ? "" : wiz.B1_9.ToString();
       
            //lB1_1.Text = (B1_1.Text == "" ? "0" : (int.Parse(B1_1.Text) * wagB1[0]).ToString());
            //lB1_2.Text = (B1_2.Text == "" ? "0" : (int.Parse(B1_2.Text) * wagB1[1]).ToString());
            //lB1_3.Text = (B1_3.Text == "" ? "0" : (int.Parse(B1_3.Text) * wagB1[2]).ToString());
            //lB1_4.Text = (B1_4.Text == "" ? "0" : (int.Parse(B1_4.Text) * wagB1[3]).ToString());
            //lB1_5.Text = (B1_1.Text == "" ? "0" : (int.Parse(B1_5.Text) * wagB1[5]).ToString());
            //lB1_6.Text = (B1_2.Text == "" ? "0" : (int.Parse(B1_2.Text) * wagB1[1]).ToString());
            //lB1_7.Text = (B1_3.Text == "" ? "0" : (int.Parse(B1_3.Text) * wagB1[2]).ToString());
            //lB1_8.Text = (B1_4.Text == "" ? "0" : (int.Parse(B1_4.Text) * wagB1[3]).ToString());
            //lB1_9.Text = (B1_4.Text == "" ? "0" : (int.Parse(B1_4.Text) * wagB1[3]).ToString());

           //string strName = "B1";
           //string strNameTemp = "";
           //for (int s = 0; s < 9; s++)//(s+1).ToString() 
           //         {
           //             strNameTemp = ("B1_"+(s+1).ToString());
           //             Label lbl= this.FindControl("l"+strNameTemp) as Label;
           //             TextBox textB= this.FindControl(strNameTemp) as TextBox;
           //           //foreach (Control c in this.Controls)
           //           //   {
           //           //       if (c is Label && c.na.== "B1_1") lbl.Text = "111";
                         
           //           //    }
                            
           //         }



            sum = 0;
            foreach (Control c in FindControlSuma(PanelB,typeof(Label)))//PanelB.Controls)//
            {
              //  for (i = 0; i < B1.Length; i++)
                for (i = 0; i < 4; i++)
                {
                    if (c.ID == B1[i])
                    {
                      //  (c as Label).Text=
                        sum = sum + int.Parse((c as Label).Text);
                    }
                }
             }
             lB1_Suma.Text = sum.ToString();
             lB1_SumaWaga.Text = (Math.Round((sum * B1Waga)/100.0,2)).ToString();


        }
        protected void Page_Load(object sender, EventArgs e)
        {
            fError.Visible = false;
            Error1.Visible = false;
            Error2.Visible = false;
            Error3.Visible = false;
            //if (Current.TrOsobaID != Current.OsobaID)
            //{
            //    komunikatError(-3);
            //    return;
            //}
            if (!IsPostBack)
            {
                //for (int i = 0; i < B1.Length ; i++) {
                //  ((Label)  B1[i].).Text="";
                // }
              vKP_Osoby item = cFirma.DaneOsoby(int.Parse(Current.TrOsobaID));
              this.NazwiskoImie.Text = item.Tytul.ToLower() +" "+
                                   FunctionGlobal.UppercaseFirstWords(item.Imie + " " + item.Nazwisko) ;
             
                if ( Szyfrowanie.EncodePassword("79") == Request["value"])// historia
                {
                    this.Podglad.Text = "HISTORIA";
                    FindControlRec(this.PanelDanePersonalne);

                    FindControlRec(this.PanelA);
                    FindControlRec(this.PanelB);
                    FindControlRec(this.PanelC);
                    FindControlRec(this.PanelD);
                    FindControlRec(this.PanelE);
                    FindControlRec(this.PanelF);
                }

                if (Szyfrowanie.EncodePassword("109") == Request["value"] )//podgląd 
                {
                    this.Podglad.Text = "PODGLĄD";
                    FindControlRec(this.PanelDanePersonalne);

                    FindControlRec(this.PanelA);
                    FindControlRec(this.PanelB);
                    FindControlRec(this.PanelC);
                    FindControlRec(this.PanelD);
                    FindControlRec(this.PanelE);
                    FindControlRec(this.PanelF);
                }
                if (Szyfrowanie.EncodePassword("211") == Request["value"])//edycja
                {
                    this.Podglad.Text = "EDYCJA";
                    FindControlRec(this.PanelE);
                    FindControlRec(this.PanelF);
                }

                //string sTe1 = Current.TrOsobaID;
                //string sTe2 = Current.OsobaID;
                //string sTe3 = Current.TrDokumentID;

                cJednostki rec= new cJednostki();
                if (Szyfrowanie.EncodePassword("79") != Request["value"])
                {
                    if (rec.PobierzAnkietaOcena(int.Parse(Current.TrDokumentID), int.Parse(Current.TrOsobaID)).Count() == 1)
                    {
                        KP_AnkietyOcenaXML wiz = rec.PobierzAnkietaOcenaXML(int.Parse(Current.TrDokumentID), int.Parse(Current.TrOsobaID));
                        if (wiz!=null) InitFormularzAnkietaOcenaXML(wiz);
                    }
                }
                else
                {
                    if (rec.PobierzHistoriaAnkietaOcenaSingleXML(int.Parse(Current.TrDokumentID)) != null)
                    {
                       // KP_AnkietyOcena wiz = rec.PobierzHistoriaAnkietaOcenaSingle(int.Parse(Current.TrDokumentID));
                        KP_AnkietyOcenaXML wiz = rec.PobierzHistoriaAnkietaOcenaSingleXML(int.Parse(Current.TrDokumentID));
                        if (wiz != null) InitFormularzAnkietaOcenaXML(wiz);
                    }
                }
             
             
            }
        }
        private void FindControlRec(Control control)
        {
            foreach (Control c in control.Controls)
            {
                TextBox x = c as TextBox;
                if (x != null)
                {
                    x.ReadOnly = true;
                }
                FindControlRec(c);
            }

        }
        private Control FindControlRec(Control where, string name)
        {
            foreach (Control c in where.Controls)
            {
                if (c.ID == name)
                    return c;
                return FindControlRec(c, name);
            }
            return null;

        }
        private IEnumerable<Control> FindControlSuma(Control control,Type type)
        {
            var controls= control.Controls.Cast<Control>();

            return controls.SelectMany(ctrl => FindControlSuma(ctrl, type))
                             .Concat(controls)
                             .Where(c => c.GetType() == type);

        }
        protected void ZapiszMojaAnkietaOcena_Click(object sender, EventArgs e)
        {
            if (Current.TrOsobaID != Current.OsobaID)
            {
                komunikatError(-3);
                return;
            }
            //Walidacja#####
                //DateTime dd1;
                //DateTime dd2;
                //if (dGrupa.SelectedValue == "0" || tOpis.Text.Trim() == "")
                //{
                //    komunikatError(-6);
                //    return;
                //}
                //if (!DateTime.TryParse(tWypelnianieOdData.Text, out dd1) || !DateTime.TryParse(tWypelnianieDoData.Text, out dd2))
                //{
                //    komunikatError(-5);
                //    return;
                //}
                //if (dd1.Date > dd2.Date || dd2.Date < DateTime.Now.Date)
                //{
                //    komunikatError(-5);
                //    return;
                //}
                cMoje dokument = new cMoje();


                AnkietaPola ap = new AnkietaPola();
                ap.nB.nB1.B1_1 = (B1_1.Text == "" ? 0 : int.Parse(B1_1.Text));
                ap.nB.nB1.B1_2 = (B1_2.Text == "" ? 0 : int.Parse(B1_2.Text));
                ap.nB.nB1.B1_3 = (B1_3.Text == "" ? 0 : int.Parse(B1_3.Text));
                ap.nB.nB1.B1_4 = (B1_4.Text == "" ? 0 : int.Parse(B1_4.Text));
                ap.nB.nB1.B1_5 = (B1_5.Text == "" ? 0 : int.Parse(B1_5.Text));
                ap.nB.nB1.B1_6 = (B1_6.Text == "" ? 0 : int.Parse(B1_6.Text));
                ap.nB.nB1.B1_7 = (B1_7.Text == "" ? 0 : int.Parse(B1_7.Text));
                ap.nB.nB1.B1_8 = (B1_8.Text == "" ? 0 : int.Parse(B1_8.Text));
                ap.nB.nB1.B1_9 = (B1_9.Text == "" ? 0 : int.Parse(B1_9.Text));
                XmlSerializer x = new XmlSerializer(typeof(AnkietaPola));
                StringWriter sw=new StringWriter();
                x.Serialize(sw, ap);

                int outProc = dokument.InsertMojaAnkietaOcenaXML(int.Parse(Current.TrDokumentID), 
                    int.Parse(Current.OsobaID), 2013, sw.GetStringBuilder().ToString());
                sw.Close();
                //int outProc = dokument.InsertMojaAnkietaOcena(int.Parse(Current.TrDokumentID), int.Parse(Current.OsobaID), 2013,
                //                B1_1.Text==""?null:(int?) int.Parse(B1_1.Text),
                //                B1_2.Text == "" ? null : (int?)int.Parse(B1_2.Text),
                //                B1_3.Text == "" ? null : (int?)int.Parse(B1_3.Text),
                //                B1_4.Text == "" ? null : (int?)int.Parse(B1_4.Text));
               
                if (outProc < 0)
                {
                    komunikatError(outProc);
                    return;
                }
               //  bWyslijAnkietaExcel.Enabled = true;
                 bAnulujAnkietaMojaOcena.Enabled = false;
                 bPowrotAnkietaMojaOcenaStronaGlowna.Enabled = true;
                 bPowrotMojaAnkietaOcenaStronaDokumenty.Enabled = true;


        }

        protected void PobierzAnkietaExcel_Click(object sender, EventArgs e)
        {
            //OpenFileDialog ofd = new OpenFileDialog();
            //ofd.InitialDirectory = @"C:\";
            ////ofd.filter = "JPEG Files(*.jpeg)|*.jpeg";
            ////ofd.title = "C# OpenFile Dialog";
            //ofd.RestoreDirectory = true;
            //if (ofd.ShowDialog() == DialogResult.OK)
            //{
            //    TextBox1010.Text = ofd.FileName;
            //} 
          
      //      bWyslijAnkietaExcel.Enabled = false;
        }
        protected void WyslijAnkietaExcel_Click(object sender, EventArgs e)
        {
       
        }

        protected void AnulujMojaAnkietaOcena_Click(object sender, EventArgs e)
        {
             Server.Transfer("~/Default.aspx");
        }
        protected void PowrotMojaAnkietaOcenaStronaGlowna_Click(object sender, EventArgs e)
        {
            Server.Transfer("~/Default.aspx");
        }

        protected void PowrotMojaAnkietaOcenaStronaDokumenty_Click(object sender, EventArgs e)
        {
            Server.Transfer("~/AnkietaOcenaPracownika/DokumentOcenaMoja.aspx");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {


        }

        protected void lInstrukcja_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pomoc/Instrukcja.pdf");
          
        }
    }
}